<?php

namespace App\Http\Controllers;

use App\Models\UserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use Intervention\Image\Facades\Image;
class UserController extends Controller
{

    public function memberOut(Request $request,UserModel $userModel){

        if ($request->isMethod('post')){

            //接收表单传过来的数据
            $id = $request->input('id');
            $data['user_name'] = $request->input('user_name');
            $data['user_password'] = $request->input('user_password');
            $data['sex'] = $request->input('sex');
            $data['age'] = $request->input('age');
            $data['phone'] = $request->input('phone');
            $file = $request->file('img');

            $relus = [
                'phone' => 'required|unique:user',
            ];

            $msg = [
              'phone.required' => '请输入手机号',
              'phone.unique'  => '该手机号已存在',
            ];
            $valid = Validator::make($data,$relus,$msg);

            if ($valid->fails()){
                $err = $valid->errors()->first();
                return "<script>alert('$err');location.href='member';</script>";
            }
            if (!$file){
                return "<script>alert('请上传头像');location.href='member';</script>";
            }else{

                //限制图片格式
                $format = ['jpg','png','jpeg','gif'];
                //设置图片名
                $ext = $file->getClientOriginalExtension();

                if (in_array($ext,$format)){

                    //图片拼接
                    $fileName = $id.'.'. $ext;

                    //图片存储路径
                    $path = __DIR__.'/../../../data/uploads/'.$id.'.jpg';

                    //图片缩略限制
                    Image::make($file->path())->resize(200,180)->save($path);

                    //将图片移动到指定储存路径下
                    $file->move($fileName,$path);

                    //接收图片存储路径
                    $data['img'] =$path;

                    //存入redis缓存
                    Redis::connection()->hset('user','users',json_encode($data));

                    $res = $userModel->memberOut($data);

                    if ($res){
                        return "<script>alert('录入成功');location.href='page';</script>";
                    }else{
                        return "<script>alert('录入失败');location.href='member';</script>";
                    }
                }
            }
        }
    }

    public function page(UserModel $userModel){
        $res = $userModel->page();

        return view("page",['res'=>$res]);
    }

    //图片展示

    public function pageImg(Request $request){
        $id = $request['id'];
        $data = UserModel::find($id);
        $img = $data['img'];
        return response()->file($img);
    }

    //图片下载

    public function down(Request $request){
        $id = $request['id'];
        $data = UserModel::find($id);
        $img = $data['img'];
        return response()->download($img,$id.'.jpg');
    }

    //删除
    public function del(Request $request,UserModel $userModel){
        $id['id'] = $request->input('id');
        $res = $userModel->del($id);
        if ($res){
            return "<script>alert('删除成功');location.href='page';</script>";
        }else{
            return "<script>alert('删除失败');location.href='page';</script>";
        }
    }


}
